#!/bin/bash
set -e

ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 用日期 (年月日时分秒)作镜像版本号
image_version=`date "+%Y-%m-%d-%H-%M-%S"`
docker build -t ${HARBOR_TEST}/hammer/$CI_PROJECT_NAME:${image_version} --file $CI_PROJECT_DIR/deploy/Dockerfile .
docker login --username "$HARBOR_USERNAME" --password "$HARBOR_PASSWORD" "$HARBOR_TEST"
docker push "$HARBOR_TEST"/hammer/$CI_PROJECT_NAME:${image_version}

mkdir -p ~/.ssh
touch ~/.ssh/config
echo -e "Host *\n\tStrictHostKeyChecking no\n\n" >> ~/.ssh/config
echo "$PRIVATE_KEY" >~/.ssh/id_rsa
echo "$PUBLIC_KEY" >~/.ssh/id_rsa.pub
chmod 0600 /root/.ssh/id_rsa

ssh -p 22 root@"$SERVER_TEST" "
docker login --username "$HARBOR_USERNAME" --password "$HARBOR_PASSWORD" "$HARBOR_TEST";
export image="$HARBOR_TEST"/hammer/"$CI_PROJECT_NAME":"$image_version"
export port=10000
docker stack deploy -c stack-front.yml --with-registry-auth $CI_PROJECT_NAME;
"
